package com.bupt.smart.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bupt.smart.model.RepairRequest;
import com.bupt.smart.smart.R;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RepairDetailsActivity extends AppCompatActivity {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.date)
    TextView date;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.img_view_1)
    ImageView img1;
    @BindView(R.id.img_view_2)
    ImageView img2;
    @BindView(R.id.img_view_3)
    ImageView img3;
    @BindView(R.id.audio_img_btn)
    ImageButton audioButton;

    private RepairRequest data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_details);
        ButterKnife.bind(this);
        initData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_repair_details, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        data = (RepairRequest)getIntent().getSerializableExtra("repairRequest");
        title.setText(data.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date.setText(sdf.format(data.getDate()));
        List<String> imgUrls = data.getImages();
        for(int i = 0; i < imgUrls.size(); i++) {
            switch (i) {
                case 0:
                    Picasso.with(getApplicationContext()).load(data.getImages().get(i)).resize(80, 80).centerCrop().into(img1);
                    break;
                case 1:
                    Picasso.with(getApplicationContext()).load(data.getImages().get(i)).resize(80, 80).centerCrop().into(img2);
                    break;
                case 2:
                    Picasso.with(getApplicationContext()).load(data.getImages().get(i)).resize(80, 80).centerCrop().into(img3);
                    break;
            }
        }
        for (int i = imgUrls.size(); i < 3; i++) {
            switch (i) {
                case 0:
                    img1.setVisibility(View.GONE);
                    break;
                case 1:
                    img2.setVisibility(View.GONE);
                    break;
                case 2:
                    img3.setVisibility(View.GONE);
                    break;
            }
        }
        content.setText(data.getContent());
    }

    @OnClick(R.id.audio_img_btn)
    public void playAudio(View v) {

    }
}
